<?php
//session_start();
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
//check if you have curl loaded
//echo $_REQUEST['json'];
if(isset($_REQUEST['id'] ) && $_REQUEST['id'] !='' && is_numeric($_REQUEST['id']))
{
  $id = $_REQUEST['id'];

	$sqltext="select title, abstract, lecture_date, duration, asset_type, abstract_url, remote_url, location, event_title, created_datetime, created_by, last_modified_datetime, last_modified_by, rating, views, thumbnail from asset WHERE id=?";
	$params = array();
	
	array_push($params,sql_escape($id));
//	array_push($params,sql_escape($user_id));
	$result = db_select_query($conn,$sqltext,$params);
	if(db_num_rows($result)<1)
	{
		//echo "Error 404 AssetId Not Found!";
		echo "HTTP/1.1 404, 403 <br />";
  	echo "Date: ".gmdate('F j, Y, g:i:s')." GMT<br />";
		echo "{\"error\": \"AssetId Not Found.\"}";
	}
	else
	{
		unset($result_array);
		unset($presenter_array);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				$result_array[$key]=$value;
				
			}
		}
	//get presenter
		/*$sqltext="SELECT first_name, last_name, middle_name, alt_name, suffix, title, department, organization FROM presenter  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$presenter_array[$key]=$value;
				$result_array["presenter"][$key]=$value;
			}
		}*/
		$sqltext="SELECT first_name, last_name, middle_name, alt_name, suffix, title, department, organization FROM presenter  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		$num=0;
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$presenter_array[$key]=$value;
				$result_array["presenters"][$num][$key]=$value;
			}
			$num++;
		}
			//get keywords
			
	/*	$sqltext="SELECT keyword FROM keyword  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$result_array[$key]=$value;
				$result_array["keywords"][$key]=$value;
			}
		}*/
		$sqltext="SELECT keyword FROM keyword  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		$num=0;
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$result_array[$key]=$value;
				$result_array['keywords'].=$value.",";
			}
			$num++;
		}
		if(isset($result_array['keywords']))
		{
			$result_array['keywords']=substr($result_array['keywords'],0,strlen($result_array['keywords'])-1);
		}
			//get timecode
		/*$sqltext="SELECT timecode, timecode_type, data FROM timecode  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$result_array[$key]=$value;
				$result_array["timecode"][$key]=$value;
			}
		}*/
		$sqltext="SELECT timecode, timecode_type, data FROM timecode  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		$num=0;
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				$result_array['timecode'][$num][$key]=$value;
			}
			$num++;
		}
				//get msc_number
		$sqltext="SELECT msc FROM msc_number  WHERE asset_id= ?";
		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		$num=0;
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				///$result_array[$key]=$value;
				$result_array['mscNumbers'].=$value.",";
			}
			$num++;
		}
		if(isset($result_array['mscNumbers']))
		{
			$result_array['mscNumbers']=substr($result_array['mscNumbers'],0,strlen($result_array['mscNumbers'])-1);
		}
		/*$sqltext="SELECT msc FROM msc_number  WHERE asset_id= ?";

		$params = array();
		array_push($params,sql_escape($id));
		$result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				//$$key = $value;
				//$result_array[$key]=$value;
				$result_array["mscNumbers"][$key]=$value;
			}
		}*/
		//print_r($result_array);
		echo "HTTP/1.1 200 OK <br />";
		echo "Date: ".gmdate('F j, Y, g:i:s')." GMT<br />";
		//echo "Content-Type: application/json<br />";
		echo "Location: ".$site_http."metadata/v1/asset/$id<br />";
		echo "{ <br />".displayTree($result_array)."<br/>}";
	}
}
?>